/**
 * Created by kim on 2015/10/8.
 */
(function ($, hb) {
  'use strict';
  var tips = {

    _show: function (el, options) {
      options = options || {};
      var $el = $(el);
      var params = {
        className: 'hb-tips',
        content: options.content || $el.prop('title'),
        showOn: 'none',
        alignTo: 'target',
        alignX: options.alignX || 'inner-left',
        offsetX: options.offsetX || parseInt($el.attr('data-tips-offsetX')) || 0,
        offsetY: options.offsetY || parseInt($el.attr('data-tips-offsetY')) || 0,
        slide: false,
        fade: false
      };
      return function (type) {
        if (type == 'top') {
          params.alignX = 'inner-left';
          params.offsetY = 5;
        } else if (type == 'left') {
          params.alignX = 'left';
          params.alignY = 'center';
          params.offsetX = 5;
        } else if (type == 'right') {
          params.alignX = 'right';
          params.alignY = 'center';
          params.offsetX = 5;
        } else if (type == 'bottom') {
          params.alignX = 'center';
          params.alignY = 'bottom';
          params.offsetY = 5;
        }
        require(['tips'], function () {
          //先隐藏已经显示的tips
          $('[data-tips]').each(function () {
            $(this).poshytip('hide');
          });
          $el.poshytip(params);
          $el.poshytip('show');
          $el.data('tips-shown', true);
        });
      };
    },

    left: function (el, options) {
      tips._show(el, options)('left');
    },

    right: function (el, options) {
      tips._show(el, options)('right');
    },

    top: function (el, options) {
      tips._show(el, options)('top');
    },

    bottom: function (el, options) {
      tips._show(el, options)('bottom');
    },

    hide: function (el) {
      var $el = $(el);
      $el.poshytip('hide');
      $el.data('tips-shown', false);
    }
  };

  //当输入框中输入内容时，隐藏掉已经显示的tips
  $(document).ready(function () {
    $('[data-tips]').each(function () {
      var $tips = $(this);
      $tips.keyup(function () {
        if ($(this).val() && $tips.poshytip) {
          $tips.poshytip('hide');
        }
      });
    });
  });
  hb.tips = tips;
})(jQuery, HB);